package com.sunwayworld.basemodule.basedata.exporttemplate.service.impl;

import com.sunwayworld.basemodule.basedata.exporttemplate.bean.LimsExportTemplateBean;
import com.sunwayworld.basemodule.basedata.exporttemplate.dao.LimsExportTemplateDao;
import com.sunwayworld.basemodule.basedata.exporttemplate.service.LimsExportTemplateService;
import com.sunwayworld.framework.mybatis.mapper.MapperParameter;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.support.PersistableHelper;
import com.sunwayworld.module.item.file.bean.CoreFileBean;
import com.sunwayworld.module.item.file.manager.CoreFileManager;
import com.sunwayworld.module.item.file.service.CoreFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.spring.annotation.GikamBean;

import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class LimsExportTemplateServiceImpl implements LimsExportTemplateService {

    @Autowired
    @Lazy
    CoreFileService coreFileService;

    @Autowired
    @Lazy
    CoreFileManager coreFileManager;

    @Autowired
    private LimsExportTemplateDao limsExportTemplateDao;

    @Override
    @SuppressWarnings("unchecked")
    public LimsExportTemplateDao getDao() {
        return limsExportTemplateDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsExportTemplateBean limsExportTemplate = jsonWrapper.parseUnique(LimsExportTemplateBean.class);
        limsExportTemplate.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(limsExportTemplate);
        return limsExportTemplate.getId();
    }

    @Override
    public LimsExportTemplateBean selectByTemplateCode(String templateCode) {
        return selectFirstByFilter(SearchFilter.instance().match("templateCode", templateCode).filter(MatchPattern.EQ));
    }

    @Override
    public List<LimsExportTemplateBean> selectTaskTemplateExport(String templateCode) {
        MapperParameter temp = new MapperParameter();
        temp.setFilter(SearchFilter.instance().match("templateCode", templateCode).filter(MatchPattern.EQ));
        List<Map<String, Object>> mapList = getDao().selectTaskTemplateExport(temp);
        if (mapList.isEmpty()) {
            return Collections.emptyList();
        }
        List<LimsExportTemplateBean> templateBeans = mapList.stream().map(m -> PersistableHelper.mapToPersistable(m, LimsExportTemplateBean.class)).collect(Collectors.toList());
        return templateBeans;
    }

    @Override
    public CoreFileBean downloadTemplate(String templateId, Long fileId) {
        CoreFileBean templateFile = coreFileService.selectById(fileId);
        if (templateFile != null) {
            templateFile.setDownloadUrl(coreFileManager.getDownloadUrl(templateFile));
            return templateFile;
        }
        return null;
    }
}
